package org.bugwatch.dice;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    
	private ImageView resultImageView;
	private Button rollButton;	
	
	
	
	public boolean rolling=false;
	
	Dice<Integer> dice;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        rollButton=(Button) findViewById(R.id.rollButton);
        
        resultImageView=(ImageView) findViewById(R.id.resultImageView);
        
        
        dice=new Dice<Integer>()
        {
        	ShortValuesDice internalDice=new ShortValuesDice();
			@Override
			public Integer roll() {
				Short roll = internalDice.roll();
				switch(roll)
				{
					case 1:
						return R.drawable.dice_1;
					case 2:
						return R.drawable.dice_2;
					case 3:
						return R.drawable.dice_3;
					case 4:
						return R.drawable.dice_4;
					case 5:
						return R.drawable.dice_5;
					case 6:
						return R.drawable.dice_6;
				}
				return -1;
			}
        	
        };
        
        rollButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
					
				if(rolling)
				{
					resultImageView.setImageResource(dice.roll());
					rolling=false;
					rollButton.setText("Roll !");
				}
				else
				{
					resultImageView.setImageResource(R.drawable.diceanimationdrawable);
					AnimationDrawable animationDrawable = (AnimationDrawable) resultImageView.getDrawable();
					animationDrawable.run();
					rolling=true;
					rollButton.setText("Stop !");
				}
			}
		});
        rollButton.setText("Roll !");
    }
}